/**
 * @author lteng
 * @time 2022-06-17 22:42:01 
 */

package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.entity.Grade;
import liteng.study.cn.stuscore.entity.Student;
import liteng.study.cn.stuscore.service.GradeService;
import liteng.study.cn.stuscore.service.StudentService;
import liteng.study.cn.stuscore.service.impl.GradeServiceImpl;
import liteng.study.cn.stuscore.service.impl.StudentServiceImpl;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private long id = 0L;
	StudentService studentService = new StudentServiceImpl();
	GradeService gradeService = new GradeServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		System.out.println("student-get方法已启动");
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		if (action.equals("info")) {
			descInfo(request, response);

		} else if (action.equals("page")) {
			pageInfo(request, response);

		} else if (action.equals("add")) {
			addInfo(request, response);

		} else if (action.equals("upd")) {
			updInfo(request, response);

		} else if (action.equals("del")) {
			delInfo(request, response);
		} else if (action.equals("addView")) {
			openAddView(request, response);
		} else if (action.equals("updView")) {
			openUpdView(request, response);

		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 */

	// TODO Auto-generated method stub
	private void descInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		long id = Long.valueOf(request.getParameter("id"));

		Student student = studentService.getOne(id);
		request.setAttribute("student", student);
		request.getRequestDispatcher("/pages/grade/upd.jsp").forward(request, response);

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void delInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		long id = Long.valueOf(request.getParameter("id"));

		Student student = studentService.getOne(id);
		

		studentService.delete(student);

		response.sendRedirect("/stuscore/student?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		int sex = Integer.valueOf(request.getParameter("sex"));
		int age = Integer.valueOf(request.getParameter("age"));
		String comm = request.getParameter("comm");
		int gid = Integer.valueOf(request.getParameter("gid"));
		long updId = Long.valueOf(request.getParameter("id"));

		if (updId != id && isExist(updId)) {
			request.setAttribute("error", "这个学号已存在！");

			request.getRequestDispatcher("student?action=updView&id=" + id).forward(request, response);
		} else {
			Student student = new Student();
			student.setId(updId);

			student.setName(name);
			student.setSex(sex);
			student.setAge(age);
			student.setComm(comm);
			student.setGid(gid);

			studentService.update(student);

			response.sendRedirect("/stuscore/student?action=page&pageIndex=1&pageSize=10");
		}

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void pageInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
//		String name = request.getParameter("name");

		String name = request.getParameter("name");
		if (name != null) {
			name = URLDecoder.decode(name, "utf-8");

		}

		Page page = null;
		if (name != null && !name.equals("")) {
			page = studentService.getPageStudent(pageIndex, pageSize, name);

		} else {
			page = studentService.getPageStudent(pageIndex, pageSize);

		}
		request.setAttribute("page", page);

		request.getRequestDispatcher("/pages/student/list.jsp").forward(request, response);

	}

	/**
	 * 修改年级信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	/*
	 * private void descInfo(HttpServletRequest request, HttpServletResponse
	 * response) throws ServletException, IOException { // TODO Auto-generated
	 * method stub
	 * 
	 * int id = Integer.valueOf(request.getParameter("id"));
	 * 
	 * Student student = studentService.getOne(id); request.setAttribute("student",
	 * student);
	 * request.getRequestDispatcher("/pages/student/upd.jsp").forward(request,
	 * response);
	 * 
	 * }
	 */

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		long id = Long.valueOf(request.getParameter("id"));

		if (isExist(id)) {
			request.setAttribute("error", "学生id已存在");
			request.getRequestDispatcher("/pages/student/add.jsp").forward(request, response);
		} else {
			String name = request.getParameter("name");
			int sex = Integer.valueOf(request.getParameter("sex"));
			int age = Integer.valueOf(request.getParameter("age"));
			String comm = request.getParameter("comm");
			int gid = Integer.valueOf(request.getParameter("gid"));

			Student student = new Student();
			student.setId(id);
			student.setName(name);
			student.setSex(sex);
			student.setAge(age);
			student.setComm(comm);
			student.setGid(gid);
			studentService.add(student);

			response.sendRedirect("/stuscore/student?action=page&pageIndex=1&pageSize=10");
		}

	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	private boolean isExist(long id) {
		// TODO Auto-generated method stub
		return studentService.isExistStudentById(id);
	}

	public void openAddView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Grade> grades = gradeService.getGrades();
		request.setAttribute("grades", grades);
		request.getRequestDispatcher("/pages/student/add.jsp").forward(request, response);

	}

	public void openUpdView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		id = Long.valueOf(request.getParameter("id"));
		Student student = studentService.getOne(id);
//		if (id!=student.getId()&&isExist(id)) {
//			request.setAttribute("error", "这个学号已经有人使用了。");
//			request.getRequestDispatcher("student?action=info&id=" + id).forward(request, response);
//		}
//		System.out.println(student);
		List<Grade> grades = gradeService.getGrades();
		request.setAttribute("grades", grades);
		request.setAttribute("student", student);

		request.getRequestDispatcher("/pages/student/upd.jsp").forward(request, response);

	}

}
